package com.txzt.gaea.upgrade.project.controller;


import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.ProgressStatus;
import com.txzt.gaea.common.util.StringUtil;
import com.txzt.gaea.upgrade.project.domain.model.ProjectProgress;
import com.txzt.gaea.upgrade.project.domain.vo.ProjectProgressVo;
import com.txzt.gaea.upgrade.project.service.IProjectProgressService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/project/progress")
public class ProjectProgressController extends BaseController {

    @Autowired
    private IProjectProgressService projectProgressService;


    /**
     * 分页查询
     *
     * @param projectProgress
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/list")
    public RtnInfo<?> projectProgress(@RequestParam(defaultValue = "1") Integer pageNumber,
                                      @RequestParam(defaultValue = "10") Integer pageSize, ProjectProgress projectProgress) {
        PageQuery<ProjectProgress> pageList = projectProgressService.pageQuery(new PageQuery<>(pageNumber, pageSize, projectProgress));
        pageList.getList().forEach(item ->{
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }


    /**
     * 项目ID查询工作进度
     *
     */
    @GetMapping(value = "/detail")
    public RtnInfo<?> list(@RequestParam Long projectId) {
        List<ProjectProgress> list = projectProgressService.selectProjectProgressByProjectId(projectId);
        list.forEach(item -> {
            item.setProgressStatusName(ProgressStatus.getInfoByCode(item.getProgressStatus()));
            item.setFinishByName(TranslateUtil.getUserName(item.getFinishBy()));
        });
        return RtnInfo.success(list);
    }

    /**
     * 完成工作进度
     *
     * @param projectProgress
     * @return
     */
    @PutMapping(value = "/finish")
    public RtnInfo<?> finish(@RequestBody ProjectProgress projectProgress) {
        //数据校验
        if(StringUtil.isBlank(projectProgress.getWorkCompletion())){
            return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"请填写工作完成情况");
        }
        if(projectProgress.getEndTime().getTime() < new Date().getTime() &&
                StringUtil.isBlank(projectProgress.getReason())){
            return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"请填写原因说明");
        }
        int count = projectProgressService.finishProjectProgress(projectProgress);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

}
